<template>
  <router-view/>
</template>
<script>
import {defineComponent, getCurrentInstance, ref, onMounted, onUnmounted} from 'vue'
import $store from "store/index"
import {message} from "ant-design-vue";

export default defineComponent({
  name: 'app',
  setup() {
    const {proxy} = getCurrentInstance()
    const _beforeUnload_time = ref('')
    const _gap_time = ref('')

    const set = () => {
      proxy.$request(proxy.$getRoute(7).route_method, proxy.$getRoute(7).route_url, {}).then(res => {
        message.success('退出登录')
        localStorage.setItem('成功', 1)
      })
      localStorage.setItem('退出', 1)
    }

    const beforeunloadHandler = () => {
      _beforeUnload_time.value = new Date().getTime();
    }
    const unloadHandler = () => {
      _gap_time.value = new Date().getTime() - _beforeUnload_time.value;
      //判断是窗口关闭还是刷新
      if (_gap_time.value <= 5) {
        set();
      }
    }

    onUnmounted(() => {
      window.removeEventListener('beforeunload', e => beforeunloadHandler(e))
      window.removeEventListener('unload', e => unloadHandler(e))

      // let isMove = false,
      //     timer = null;
      // window.onmousemove = function () {
      //   isMove = true;
      //   clearTimeout(timer);
      //   console.log(isMove); // 移动时
      //   timer = setTimeout(function () {
      //     isMove = false;
      //     console.log(isMove); // 静止后
      //   }, 200);
      //   console.log(1);
      // }
    })


    onMounted(() => {
          //  页面加载是获取sessionStorage里的状态信息
          let allStore = sessionStorage.getItem("store") ? sessionStorage.getItem("store") : ""
          if (allStore) {
            $store.replaceState(Object.assign({}, $store.state, JSON.parse(allStore)))
            // sessionStorage.removeItem("store")
          }
          window.addEventListener("beforeunload", () => {
            if (proxy.$cookie.getCookie("user") === null) {
              // sessionStorage.removeItem("store")
            } else {
              sessionStorage.setItem("store", JSON.stringify($store.state))
            }
          })
          window.addEventListener('beforeunload', e => beforeunloadHandler(e))
          window.addEventListener('unload', e => unloadHandler(e))
        }
    )
  }
})
</script>
<style>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}

:root {
  font-family: "PingFang SC-Regular", "PingFang SC", "PingFang SC-Medium", "PingFang Bold";
}
</style>
